package com.gy.zpchat.ui.user;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.Collections;

import im.zego.zim.callback.ZIMUserAvatarUrlUpdatedCallback;
import im.zego.zim.callback.ZIMUserNameUpdatedCallback;
import im.zego.zim.callback.ZIMUsersInfoQueriedCallback;
import im.zego.zim.entity.ZIMError;
import im.zego.zim.entity.ZIMErrorUserInfo;
import im.zego.zim.entity.ZIMUserFullInfo;
import im.zego.zim.enums.ZIMErrorCode;
import com.gy.zpchat.MyApplication;
import com.gy.zpchat.R;
import com.gy.zpchat.SDKManager;
import com.gy.zpchat.ui.meeting.MeetingListActivity;
import com.gy.zpchat.util.AppLogger;
import com.gy.zpchat.util.GlideUtils;
import im.zego.zpns.ZPNsManager;

public class ZIMUserFragment extends Fragment {

    private static final String TAG = "ZIMUserFragment";
    private ImageView img_avatar;
    private TextView tvUserName;
    private Button btnMeetingList;
    private Button clearBadge;
    private static final String IMAGE_URL = "https://storage.zego.im/zim/example/web/assets/%d.jpeg";
    private int currentImageIndex = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
        img_avatar = rootView.findViewById(R.id.image_avatar);
        tvUserName = rootView.findViewById(R.id.tv_username);
        btnMeetingList = rootView.findViewById(R.id.btn_meeting_list);
        clearBadge = rootView.findViewById(R.id.btn_clear_badge);
        SDKManager.sharedInstance().queryUsersInfo(Collections.singletonList(MyApplication.sUserId),true, new ZIMUsersInfoQueriedCallback() {
            @Override
            public void onUsersInfoQueried(ArrayList<ZIMUserFullInfo> userList, ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
                AppLogger.getInstance().d("onUsersInfoQueried,errorInfo:%s,errorMsg:%s,userList:%d,errorUserList:%d"
                        , errorInfo.code.value(), errorInfo.message, userList.size(), errorUserList.size());
                if (errorInfo.code == ZIMErrorCode.SUCCESS && getActivity() != null && !userList.isEmpty()) {
                    AppLogger.getInstance().d("onUsersInfoQueried,avatarUrl:%s", userList.get(0).userAvatarUrl);
                    setAvatar(userList.get(0).userAvatarUrl);
                    setUserName(userList.get(0).baseInfo.userName);
                }
            }
        });
        clearBadge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ZPNsManager.getInstance().setApplicationIconBadgeNumber(getContext(),0);
            }
        });
        img_avatar.setOnClickListener(v -> {
            SDKManager.sharedInstance().updateUserAvatar(String.format(IMAGE_URL, currentImageIndex), new ZIMUserAvatarUrlUpdatedCallback() {
                @Override
                public void onUserAvatarUrlUpdated(String userAvatarUrl, ZIMError errorInfo) {
                    if (errorInfo.code == ZIMErrorCode.SUCCESS) {
                        setAvatar(userAvatarUrl);
                    }
                }
            });
            currentImageIndex++;
            if (currentImageIndex > 5) {
                currentImageIndex = 1;
            }
        });
        tvUserName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText = new EditText(getContext());
                new AlertDialog.Builder(getContext())
                        .setTitle("Modify userName")
                        .setView(editText)
                        .setPositiveButton("ok", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                String userName = editText.getText().toString();
                                SDKManager.sharedInstance().updateUserName(userName, new ZIMUserNameUpdatedCallback() {
                                    @Override
                                    public void onUserNameUpdated(String userName, ZIMError errorInfo) {
                                        if (errorInfo.code == ZIMErrorCode.SUCCESS) {
                                            setUserName(userName);
                                        }
                                    }
                                });
                                dialog.dismiss();
                            }
                        })
                        .setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
        btnMeetingList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MeetingListActivity.actionStart(getActivity());
            }
        });
        return rootView;
    }

    private void setUserName(String userName) {
        AppLogger.getInstance().d("set user name:%s", userName);
        if (!TextUtils.isEmpty(userName)) {
            tvUserName.setText(userName);
        }
    }

    private void setAvatar(String url) {
        if (getActivity() == null) {
            return;
        }
        GlideUtils.loadUrl(img_avatar, url);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }
}
